<template>
    <div>
        <el-row class=" border_bottom" type="flex" justify="start">
            <el-col :span="2">
                <h3> 添加管理员</h3>
            </el-col>
        </el-row>
        <el-row>
            <el-col :span="8">
                <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px"
                         class="demo-ruleForm">
                    <el-form-item label="用户名" prop="adminName">
                        <el-input v-model.number="ruleForm.adminName"></el-input>
                    </el-form-item>
                    <el-form-item label="密码" prop="adminPassword">
                        <el-input type="password" v-model="ruleForm.adminPassword" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="确认密码" prop="checkPass">
                        <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="角色选择：" prop="roleArr">
                        <el-checkbox-group v-model="ruleForm.roles">
                            <template v-for="item in ruleForm.roleArr">
                                <el-checkbox :label="item.roleId">{{ item.roleName }}</el-checkbox>
                            </template>
                        </el-checkbox-group>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                        <el-button @click="resetForm('ruleForm')">重置</el-button>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
    </div>
</template>
<script>
    export default {
        name: "AddManager",
        data() {
            var checkAge = (rule, value, callback) => {
                if (!value) {
                    return callback(new Error('用户名不能为空'));
                }
                setTimeout(() => {
                    // 当前用户名是否存在
                    //做判断
                    this.$axios.post('/api/getManager', {userName: value}).then((res) => {
                        console.log(res);
                        if (res.data.code === 0) {
                            callback(new Error('用户名已经存在'));
                        } else {
                            callback();
                        }
                    })
                }, 1000);
            };
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (this.ruleForm.checkPass !== '') {
                        this.$refs.ruleForm.validateField('checkPass');
                    }
                    callback();
                }
            };
            var validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.ruleForm.adminPassword) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            return {

                ruleForm: {
                    adminPassword: '',
                    checkPass: '',
                    adminName: '',
                    quanxian: '',
                    roleArr: [],
                    roles: [],
                },

                rules: {
                    adminPassword: [
                        {validator: validatePass, trigger: 'blur'}
                    ],
                    checkPass: [
                        {validator: validatePass2, trigger: 'blur'}
                    ],
                    userName: [
                        {validator: checkAge, trigger: 'blur'}
                    ]
                }
            };
        },
        methods: {
            open2() {
                this.$message({
                    message: '添加成功！',
                    type: 'success'
                });
            },
            open4() {
                this.$message.error('很遗憾，添加失败了！');
            },
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        //提交成功
                        console.log(this.ruleForm.roles);
                        this.$axios.post('/api/admin/add?adminName=' + this.ruleForm.adminName + '&adminPassword=' + this.ruleForm.adminPassword + '&roleIds=', [...this.ruleForm.roles]).then((res) => {
                            console.log(res);
                        })
                        this.open2()
                    } else {
                        this.open4()
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
        },
        created() {
            //请求当前所有角色
            this.$axios.post('/api/admin/getRole').then((res) => {
                console.log('**************************************************************')
                this.ruleForm.roleArr = res.data.data.slice(1)
                console.log(this.ruleForm.roleArr)


            })
        }
    }
</script>

<style scoped>
    .border_bottom {

        border-bottom: 1px solid grey;
    }
</style>